const { Location } = require('../../database/index')
const { getExecutedSql } = require('../../logger');
// 导出
module.exports = async (req, res) =>
{
    const operation = req.params.operation;
    const { locationID, locationName, locationDescription } = req.body
    try {
        switch (operation) {
            case 'create':
                // // 根据客户端传递过来的 用户名 查询 数据库 中 是否 存在这用户名
                const state = {
                    LocationName: locationName,
                    LocationDescription: locationDescription
                }
                const newLocationData = await Location.create(state)
                const data = await Location.findOne({
                    where: {
                        LocationID: newLocationData.LocationID
                    }
                })
                res.status(200).send({
                    data,
                    meta: {
                        msg: "创建成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'read':
                const locationData = await Location.findAll()
                res.status(200).send({
                    data: locationData,
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'update':
                const stateData = {
                    LocationName: locationName,
                    LocationDescription: locationDescription
                }
                const upEmployeeData = await Location.update(stateData, { where: { LocationID: locationID } },)
                res.status(200).send({
                    meta: {
                        msg: "编辑成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'delete':
                const delData = await Location.destroy({ where: { LocationID: locationID } })
                res.status(200).send({
                    meta: {
                        msg: "删除成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break
            default:
                break;
        }

    } catch (error) {
        res.status(500).send({ message: 'Internal server error', error: error.message });
    }
}